package ta.web.servlet;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import ta.dao.CategoryDao;
import ta.dao.BlogDao;
import ta.pojo.Category;
import ta.pojo.Blog;
import ta.utils.PageInfo;

@WebServlet("/admin/BlogServlet")
public class BlogServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String type = request.getParameter("type");
        switch (type)
        {
            case "add":
                add(request, response);
                break;

            case "add1":
                add1(request,response);
                break;

            case "del":
                del(request, response);
                break;

            case "edit":
                edit(request, response);
                break;

            case "getlist":
                getList(request, response);
                break;

            case "getlist1":
                getList1(request, response);
                break;

            case "getbyid":
                getById(request, response);
                break;
        }
    }

    // 添加
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> map = request.getParameterMap();
        Blog blog = new Blog();
        try
        {
            BeanUtils.populate(blog, map);
        } catch (IllegalAccessException | InvocationTargetException e)
        {
            e.printStackTrace();
        }
        blog.setAuthor((String)request.getSession().getAttribute("loginame"));
        LocalDateTime date=LocalDateTime.now();
        DateTimeFormatter sdf=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        blog.setPubtime(date.format(sdf));
        BlogDao dao = new BlogDao();
        dao.add(blog);
        // 跳转到管理页面
        response.sendRedirect("BlogServlet?type=getlist&page=1");
    }

    protected void add1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> map = request.getParameterMap();
        Blog blog = new Blog();
        try
        {
            BeanUtils.populate(blog, map);
        } catch (IllegalAccessException | InvocationTargetException e)
        {
            e.printStackTrace();
        }
        blog.setAuthor((String)request.getSession().getAttribute("loginame"));
        LocalDateTime date=LocalDateTime.now();
        DateTimeFormatter sdf=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        blog.setPubtime(date.format(sdf));
        BlogDao dao = new BlogDao();
        dao.add(blog);
        // 跳转到页面
        response.sendRedirect("../Index?type=getlist");
    }

    // 删除
    protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String blogid = request.getParameter("blogid");
        BlogDao dao = new BlogDao();
        dao.del(blogid);
        response.sendRedirect("BlogServlet?type=getlist&page=1");
    }

    // 修改
    protected void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> map = request.getParameterMap();
        Blog blog = new Blog();
        try
        {
            BeanUtils.populate(blog, map);
        } catch (IllegalAccessException | InvocationTargetException e)
        {
            e.printStackTrace();
        }
        BlogDao dao = new BlogDao();
        dao.edit(blog);
        response.sendRedirect("BlogServlet?type=getlist&page=1");
    }

    // 搜索和分页查询
    protected void getList(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String keyword = request.getParameter("keyword");// null--""
        String currentpage = request.getParameter("page");

        int page1 = currentpage == null ? 1 : Integer.valueOf(currentpage);
        keyword = keyword == null ? "" : keyword;
        BlogDao dao = new BlogDao();
        PageInfo<Blog> pb = new PageInfo<Blog>();
        pb.setCurrentpage(page1);
        dao.getAll(keyword, pb);

        CategoryDao dao1=new CategoryDao();
        List<Category>  list =dao1.getAll();
        request.setAttribute("list", list);
        request.setAttribute("pb", pb);
        request.setAttribute("keyword", keyword);
        request.getRequestDispatcher("blog/list.jsp").forward(request, response);

    }
    protected void getList1(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        BlogDao dao = new BlogDao();
        List<Blog> blogAll= dao.getAll();
        CategoryDao dao1=new CategoryDao();
        List<Category>  list =dao1.getAll();
        request.setAttribute("list", list);
        request.setAttribute("blogAll", blogAll);
        request.getRequestDispatcher("../indexlogin.jsp").forward(request, response);

    }

    protected void getById(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String blogid = request.getParameter("blogid");
        BlogDao dao = new BlogDao();
        Blog blog = dao.getById(blogid);
        CategoryDao dao1=new CategoryDao();
        List<Category>  list =dao1.getAll();
        request.setAttribute("list", list);
        request.setAttribute("blog", blog);
        request.getRequestDispatcher("blog/edit.jsp").forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}
